/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PurchaseInvoicePayment.java
 *
 * Created on Dec 20, 2010, 4:37:59 PM
 */
package ui.rep;

import com.nanosl.lib.date.JXDatePicker;
import com.nanosl.lib.log.Loggings;
import ent.Bank;
import ent.IssuedCash;
import ent.IssuedCheque;
import ent.PurchaseInvoice;
import java.awt.Component;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import util.SuperView;

/**
 *
 * @author t
 */
public class RepPaymentView extends SuperView {

    private static RepPaymentView instance = new RepPaymentView();

    public static void display() {
        getInstance().setVisible(true);
    }

    /**
     * @return the instance
     */
    public static RepPaymentView getInstance() {
        return instance;
    }

    public RepPaymentView() {
        onLoad();
    }

    /**
     * Creates new form PurchaseInvoicePayment
     */
    public RepPaymentView(java.awt.Frame parent, boolean modal) {
        this();
        onLoad();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        dateTable = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        remainingAmountField = new javax.swing.JTextField();
        invoiceNumberField = new javax.swing.JTextField();
        supplierNameField = new javax.swing.JTextField();
        amountField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        invoiceDateField = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        paymentAmountTextField = new javax.swing.JTextField();
        chequeNumbertField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        bankingDatePicker = new JXDatePicker();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        paymentTable = new javax.swing.JTable();
        bankComboBox = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        processButton = new javax.swing.JButton();
        clearButton = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        repComboBox = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("PURCHASE PAYMENT");

        dateTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Date"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        dateTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                dateTableMouseReleased(evt);
            }
        });
        dateTable.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                dateTableKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(dateTable);
        dateTable.getColumnModel().getColumn(0).setResizable(false);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Selected Invoice"));

        remainingAmountField.setEditable(false);
        remainingAmountField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);

        invoiceNumberField.setEnabled(false);

        supplierNameField.setEnabled(false);

        amountField.setEditable(false);
        amountField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);

        jLabel3.setText("Amount:");

        jLabel1.setText("Number:");

        jLabel4.setText("Remaining:");

        jLabel2.setText("Supplier:");

        jLabel8.setText("Date:");

        invoiceDateField.setEnabled(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(invoiceNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(amountField, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 113, Short.MAX_VALUE)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(remainingAmountField, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(invoiceDateField, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(supplierNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {amountField, invoiceNumberField, remainingAmountField});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(invoiceNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(invoiceDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(supplierNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(amountField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(remainingAmountField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Payment"));

        paymentAmountTextField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        paymentAmountTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                paymentAmountTextFieldActionPerformed(evt);
            }
        });

        chequeNumbertField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chequeNumbertFieldActionPerformed(evt);
            }
        });

        jLabel7.setText("Banking Date:");

        jLabel6.setText("Cheque Number:");

        jLabel5.setText("Amount:");

        paymentTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Amount", "Cheque Number", "Banking Date", "Bank"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        paymentTable.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                paymentTableKeyReleased(evt);
            }
        });
        jScrollPane2.setViewportView(paymentTable);
        paymentTable.getColumnModel().getColumn(0).setCellRenderer(rightAlignCell);
        paymentTable.getColumnModel().getColumn(1).setCellRenderer(null);
        paymentTable.getColumnModel().getColumn(2).setCellRenderer(null);

        jLabel9.setText("Bank:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(paymentAmountTextField)
                            .addComponent(chequeNumbertField, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(bankingDatePicker, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(bankComboBox, 0, 177, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(chequeNumbertField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(bankingDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(paymentAmountTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bankComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        processButton.setText("Process");
        processButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                processButtonActionPerformed(evt);
            }
        });

        clearButton.setText("Clear");
        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });

        jLabel10.setText("Rep:");

        repComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                repComboBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addGap(18, 18, 18)
                        .addComponent(repComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(clearButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(processButton))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {clearButton, processButton});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(repComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(processButton)
                    .addComponent(clearButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void dateTableKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_dateTableKeyReleased
        fill();
    }//GEN-LAST:event_dateTableKeyReleased

    private void dateTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_dateTableMouseReleased
        fill();
    }//GEN-LAST:event_dateTableMouseReleased

    private void paymentAmountTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_paymentAmountTextFieldActionPerformed
        addPay();
    }//GEN-LAST:event_paymentAmountTextFieldActionPerformed

    private void paymentTableKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_paymentTableKeyReleased
        if (evt.getKeyCode() == 127) {
            deletePaymentRow(paymentTable.getSelectedRow());
        }
    }//GEN-LAST:event_paymentTableKeyReleased

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed
        clearAll();
    }//GEN-LAST:event_clearButtonActionPerformed

    private void processButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_processButtonActionPerformed
        process();
    }//GEN-LAST:event_processButtonActionPerformed

    private void repComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_repComboBoxActionPerformed
        fillDates();
}//GEN-LAST:event_repComboBoxActionPerformed

    private void chequeNumbertFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chequeNumbertFieldActionPerformed
        if (!chequeNumbertField.getText().trim().equals(""));
        {
            bankingDatePicker.requestFocus();
        }
    }//GEN-LAST:event_chequeNumbertFieldActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                RepPaymentView dialog = new RepPaymentView(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField amountField;
    private javax.swing.JComboBox bankComboBox;
    private org.jdesktop.swingx.JXDatePicker bankingDatePicker;
    private javax.swing.JTextField chequeNumbertField;
    private javax.swing.JButton clearButton;
    private javax.swing.JTable dateTable;
    private javax.swing.JTextField invoiceDateField;
    private javax.swing.JTextField invoiceNumberField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField paymentAmountTextField;
    private javax.swing.JTable paymentTable;
    private javax.swing.JButton processButton;
    private javax.swing.JTextField remainingAmountField;
    private javax.swing.JComboBox repComboBox;
    private javax.swing.JTextField supplierNameField;
    // End of variables declaration//GEN-END:variables
    List<PurchaseInvoice> purchaseInvoices;
    List<Bank> banks;
    DefaultTableModel dateDtm;
    DefaultTableModel paymentDtm;
    PurchaseInvoice purchaseInvoice;

    protected void onLoad() {
        initComponents();
        dateDtm = (DefaultTableModel) dateTable.getModel();
        paymentDtm = (DefaultTableModel) paymentTable.getModel();
    }

    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
        clearAll();
    }

    private void fillInvoiceTable() {
        dateDtm.setRowCount(0);
        paymentDtm.setRowCount(0);
        purchaseInvoices = m.find(PurchaseInvoice.class);
        for (Iterator<PurchaseInvoice> it = purchaseInvoices.iterator(); it.hasNext();) {
            PurchaseInvoice pi = it.next();
            double credit = pi.getCredit() == null ? 0 : pi.getCredit();
            if (credit > 0) {
                Object[] rowData = {pi.getPurchaseInvoicePK().getInvNo()};
                dateDtm.addRow(rowData);
            }
        }
    }

    private void fill() {
        int selectedRow = dateTable.getSelectedRow();
        if (selectedRow > -1) {
            fill(purchaseInvoices.get(selectedRow));
        }
    }

    public void fill(PurchaseInvoice pi) {
        clearAll();
        purchaseInvoice = pi;
        invoiceNumberField.setText(purchaseInvoice.getPurchaseInvoicePK().getInvNo());
        invoiceDateField.setText(yyyy_MM_dd.format(purchaseInvoice.getInvDate()));
        supplierNameField.setText(purchaseInvoice.getSupplier().getName());
        amountField.setText(nf2d.format(purchaseInvoice.getAmount()));
        remainingAmountField.setText(nf2d.format(purchaseInvoice.getCredit()));
    }

    private void addPay() {
        if (purchaseInvoice == null) {
            return;
        }
        try {
            double paymentAmount = Double.valueOf(paymentAmountTextField.getText().trim());
            if (paymentAmount > 0) {
                String chequeNumber = chequeNumbertField.getText().trim();
                if (!chequeNumber.equals("")) {
                    Date bankingDate = bankingDatePicker.getDate();
                    if (bankingDate != null) {
                        Object[] row = {nf2d.format(paymentAmount), chequeNumber, yyyy_MM_dd.format(bankingDate), banks.get(bankComboBox.getSelectedIndex()).getCode()};
                        fillPaymentTable(row);
                    }
                } else {
                    for (int i = 0; i < paymentTable.getRowCount(); i++) {
                        if (paymentTable.getValueAt(i, 1) == null) {
                            paymentAmount += Double.valueOf(paymentTable.getValueAt(i, 0).toString());
                            paymentTable.setValueAt(nf2d.format(paymentAmount), i, 0);
                            clearPaymentFields();
                            calcRemaining();
                            return;
                        }
                    }
                    Object[] row = {nf2d.format(paymentAmount)};
                    fillPaymentTable(row);
                }
            }
        } catch (NumberFormatException e) {
        } catch (Exception e) {
            Loggings.logError(getName(), e);
        }
    }

    private void fillPaymentTable(Object[] row) {
        paymentDtm.addRow(row);
        clearPaymentFields();
        calcRemaining();
    }

    private void deletePaymentRow(int selectedRow) {
        if (selectedRow > -1) {
            paymentDtm.removeRow(selectedRow);
            calcRemaining();
        }
    }

    private void calcRemaining() {
        double remaining = purchaseInvoice.getCredit();
        int rowCount = paymentTable.getRowCount();
        for (int i = 0; i < rowCount; i++) {
            remaining = remaining - Double.valueOf(paymentTable.getValueAt(i, 0).toString());
        }
        remainingAmountField.setText(nf2d.format(remaining));
    }

    private void clearPaymentFields() {
        chequeNumbertField.setText("");
        paymentAmountTextField.setText("");
    }

    private void clearAll() {
        purchaseInvoice = null;
        invoiceNumberField.setText("");
        invoiceDateField.setText("");
        supplierNameField.setText("");
        amountField.setText("");
        remainingAmountField.setText("");
        clearPaymentFields();
        paymentDtm.setRowCount(0);
        fillInvoiceTable();
        fillBanks();
    }

    private void process() {
        int rowCount = paymentTable.getRowCount();
        if (purchaseInvoice == null || rowCount == 0) {
            return;
        }
        List<Serializable> serializables = new ArrayList<Serializable>();
        try {
            for (int i = 0; i < rowCount; i++) {
                Object o = paymentTable.getValueAt(i, 1);
                String chequeNumber = o == null ? "" : o.toString().trim();
                double amount = Double.valueOf(paymentTable.getValueAt(i, 0).toString());
                if (chequeNumber.equals("")) {
                    IssuedCash issuedCash = new IssuedCash(new Date());
                    issuedCash.setAmount(amount);
                    issuedCash.setPurchaseInvoice(purchaseInvoice);
                    serializables.add(issuedCash);
                    purchaseInvoice.getIssuedCashCollection().add(issuedCash);
                } else {
                    Date bankingDate = yyyy_MM_dd.parse(paymentTable.getValueAt(i, 2).toString());
                    IssuedCheque issuedCheque = new IssuedCheque(chequeNumber);
                    issuedCheque.setAmount(amount);
                    issuedCheque.setBank(new Bank(paymentTable.getValueAt(i, 3).toString()));
                    issuedCheque.setBankingDate(bankingDate);
                    issuedCheque.setIssuedDate(new Date());
                    issuedCheque.setPurchaseInvoice(purchaseInvoice);
                    issuedCheque.setStatus(0);
                    serializables.add(issuedCheque);
                    purchaseInvoice.getIssuedChequeCollection().add(issuedCheque);
                }
            }
        } catch (ParseException ex) {
            showError("Problem with one of cheque banking date!");
            return;
        } catch (Exception ex) {
            showError("Unable to complete payment");
            return;
        }
        double remainingAmount = Double.valueOf(remainingAmountField.getText().trim());
        purchaseInvoice.setCredit(remainingAmount);
        serializables.add(purchaseInvoice);
        if (m.update(serializables)) {
            clearAll();
        } else {
            showError("Unable to complete payment!");
        }
    }

    private void fillBanks() {
        bankComboBox.setModel(new DefaultComboBoxModel(m.find(Bank.class).toArray()));
    }

    private void fillDates() {
        dateDtm.setRowCount(0);
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void KeyAdapter() {

        AutoCompleteDecorator.decorate(bankComboBox);
        setComboBoxKeyAdapters(bankComboBox);
        AutoCompleteDecorator.decorate(repComboBox);
        setComboBoxKeyAdapters(repComboBox);


    }

    private void setComboBoxKeyAdapters(JComboBox comp) {
        String compName = comp.getName();
        Component component[] = comp.getComponents();
        for (int i = 0; i < component.length; i++) {
            if (comp.equals("itemComboBox")) {
                component[i].addKeyListener(bankComboBoxKeyAdapter);
            }
            if (comp.equals("repComboBox")) {
                component[i].addKeyListener(repComboBoxKeyAdapter);
            }
        }

    }
    KeyAdapter bankComboBoxKeyAdapter = new java.awt.event.KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent evt) {
            bankComboBoxKeyAdapter.keyPressed(evt);
        }
    };
    KeyAdapter repComboBoxKeyAdapter = new java.awt.event.KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent evt) {
            repComboBoxKeyAdapter.keyPressed(evt);
        }
    };
}
